package getter

import (
	"server/game/cache"
	"server/game/model"

	"github.com/hwcer/cosgo/registry"
	"github.com/hwcer/yyds/context"
)

func init() {
	Register(&data{})
}

/*
getter 数据获取
*/

type data struct {
}

func (this *data) Caller(node *registry.Node, handle *context.Context) interface{} {
	f := node.Method().(func(*data, *context.Context) interface{})
	return f(this, handle)
}

/**
 * @name role
 * 基本信息
 */
func (this *data) Role(c *context.Context) interface{} {
	doc := cache.GetRole(c.Player.Updater)
	return doc.All()
}

/**
 * @name item
 * @param int bag 背包ID
 * 道具列表,标准分页
 */
func (this *data) Item(c *context.Context) (r any) {
	return GetItemsWithPage(c)
}

// Daily 日常数据
//
// 最多14天
//func (this *data) Daily(c *context.Context) interface{} {
//	var rows []*model.Daily
//	iid, _ := times.New(c.Now()).Sign(0)
//	his := c.Player.Collection(config.ITypeHistory)
//	his.Range(func(id string, doc *dataset.Document) bool {
//		v := doc.Any().(*model.History)
//		if v.IID != iid {
//			rows = append(rows, &model.Daily{History: *v})
//		}
//		return true
//	})
//	//附件今日所有对象
//	day := c.Player.Document(config.ITypeDaily)
//	v := day.Any().(*model.Daily)
//	rows = append(rows, v)
//	sort.Slice(rows, func(i, j int) bool {
//		return rows[i].IID > rows[j].IID
//	})
//	return rows
//}

/**
 * @name active
 * @tip type=1-游戏公告,2-游戏充值,8-经典活动,9-超级邮件,88-超级活动,100-兑换码
 * 活动数据
 */

//func (this *data) Active(c *context.Context) interface{} {
//	return make(map[string]any)
//}

/**
 * @name record
 * 成就数据
 */

//func (this *data) Record(c *context.Context) interface{} {
//	r := make(map[string]any)
//	items := cache.GetItems(c.Player.Updater)
//	items.RangeWithIType(config.ITypeRecord, func(id string, item *model.Items) bool {
//		r[id] = map[string]any{"name": id, "val": item.Value, "_id": id}
//		return true
//	})
//	return nil
//}

/**
 * @name collect
 * @param int bag 图鉴ID，留空获取所有
 * 图鉴数据
 */

func (this *data) Collect(c *context.Context) interface{} {
	var rows []*model.Collect
	tx := db.Where("uid = ?", c.Uid()).Order("update", -1).Find(&rows)
	if tx.Error != nil {
		return tx.Error
	}
	return rows
}

/**
 * @name otherCollect
 * @param int bag 图鉴ID，留空获取所有
 * @param string uid  图鉴ID
 * 获取他人的图鉴数据
 */

func (this *data) OtherCollect(c *context.Context) interface{} {
	uid := c.GetString("uid")
	var rows []*model.Collect
	tx := db.Where("uid = ?", uid)
	if bag := c.GetInt32("bag"); bag > 0 {
		tx = tx.Where("bag = ?", bag)
	}
	tx = tx.Limit(100)
	tx = tx.Order("update", -1).Find(&rows)
	if tx.Error != nil {
		return tx.Error
	}
	return rows
}

/**
 * @name GetCollectCount
 * @param int bag 图鉴ID，留空获取所有
 * @param string uid  图鉴ID
 * 获取他人的图鉴数据收集数量
 */
func (this *data) GetCollectCount(c *context.Context) interface{} {
	uid := c.GetString("uid")
	bag := c.GetInt32("bag")

	var v int64
	tx := db.Model(&model.Collect{}).Where("uid = ?", uid)
	tx = tx.Where("bag = ?", bag).Count(&v)
	if tx.Error != nil {
		return tx.Error
	}
	return v
}
